package com.mercury.ec.module.air.dal.mysql.order;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mercury.ec.framework.common.enums.CommonStatusEnum;
import com.mercury.ec.framework.mybatis.core.mapper.BaseMapperX;
import com.mercury.ec.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.mercury.ec.module.air.controller.admin.order.vo.order.AirOrderPageReqVO;
import com.mercury.ec.module.air.controller.admin.order.vo.order.AirOrderRespVO;
import com.mercury.ec.module.air.dal.dataobject.order.AirOrderDO;
import com.mercury.ec.module.air.enums.AirOrderStatusEnum;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;

/**
 * 机票订单 Mapper
 *
 * @author hesx
 */
@Mapper
public interface AirOrderMapper extends BaseMapperX<AirOrderDO> {

    IPage<AirOrderRespVO> selectOrderPage(IPage<AirOrderRespVO> page, @Param("query") AirOrderPageReqVO reqVO);

    AirOrderRespVO selectOrderById(Long id);

    default AirOrderDO selectByNo(String no) {
        return selectOne(AirOrderDO::getNo, no);
    }

    /**
     * 编码会回收，取180天内最新的订单
     *
     * @param pnr
     * @return
     */
    default AirOrderDO selectByPnr(String pnr) {
        return selectOne(new LambdaQueryWrapperX<AirOrderDO>()
                .eq(AirOrderDO::getPnr, pnr)
                .ne(AirOrderDO::getStatus, AirOrderStatusEnum.CANCELED.getStatus())
                .between(AirOrderDO::getCreateTime, LocalDateTimeUtil.offset(LocalDateTime.now(), -180, ChronoUnit.DAYS), LocalDateTime.now())
                .orderByDesc(AirOrderDO::getCreateTime), false);
    }
}
